<template>
  <transition name="fade" @after-leave="onAfterLeave">
    <div v-if="visible" class="message">
      <span>{{ message }}</span>
      <button @click="close">关闭</button>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'c-message',
  data() {
    return {
      visible: false,
      message: '你好',
      duration: 3000,
      onClose: null
    }
  },
  mounted() {

  },
  methods: {
    close() {
      if (this.onClose == null) return
      this.onClose()
      this.visible = false
    },
    onAfterLeave() {
      this.$destroy()
      this.$el.parentNode.removeChild(this.$el)
    }
  }
}
</script>

<style>
.message {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 200px;
  height: 50px;
  padding: 0 10px;
  border: 1px solid grey;
  border-radius: 4px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>